import requests
import unittest



class TPShopLogin(unittest.TestCase):

    def setUp(self):
        self.session = requests.Session()
        self.url_verify = '''http://10.10.1.1/index.php?m=Home&c=User&a=verify'''
        self.url_login = '''http://10.10.1.1/index.php?m=Home&c=User&a=do_login'''

    def tearDown(self):
        self.session.close()


    def test01_success(self):
        response = self.session.get(url=self.url_verify)
        # 结果断言
        self.assertEqual(200, response.status_code)  # 断言相应状态码200
        self.assertIn("image", response.headers.get("Content-Type"))

        # 登录测试
        login_data = {
            'username': "18177542589",
            'password': "123456",
            'verify_code': '8888'
        }
        response = self.session.post(url=self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, response.json().get('status'))
        self.assertIn("登陆成功", response.json().get("msg"))

    def test02_user_isnot_exist(self):
        response = self.session.get(url=self.url_verify)
        # 结果断言
        self.assertEqual(200, response.status_code)  # 断言相应状态码200
        self.assertIn("image", response.headers.get("Content-Type"))

        # 登录测试
        login_data = {
            'username': "1817754999",
            'password': "123456",
            'verify_code': '8888'
        }
        response = self.session.post(url=self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-1, response.json().get('status'))
        self.assertIn("账号不存在", response.json().get("msg"))

    def test03_password_error(self):
        response = self.session.get(url=self.url_verify)
        # 结果断言
        self.assertEqual(200, response.status_code)  # 断言相应状态码200
        self.assertIn("image", response.headers.get("Content-Type"))

        # 登录测试
        login_data = {
            'username': "18177542589",
            'password': "888888",
            'verify_code': '8888'
        }
        response = self.session.post(url=self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-2, response.json().get('status'))
        self.assertIn("密码错误", response.json().get("msg"))



